package com.zsinfo.guoss.supplier.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.ErrorBean;
import com.example.netcenter.bean.VersionBean;
import com.example.utilcenter.updateTip.updateTip;
import com.google.gson.Gson;
import com.zsinfo.guoss.supplier.MyApplication;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.DataCleanManager;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;
import com.zsinfo.guoss.supplier.utils.UIUtils;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;

public class SettingActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.rl_app_version)
    RelativeLayout rl_app_version;
    @BindView(R.id.rl_clean)
    RelativeLayout rl_clean;

    @BindView(R.id.rl_update_pwd)
    RelativeLayout rl_update_pwd;

    @BindView(R.id.rl_pay_pwd)
    RelativeLayout rl_pay_pwd;
    @BindView(R.id.rl_aboutUs)
    RelativeLayout rl_aboutUs;

    @BindView(R.id.tv_version)
    TextView tv_version;
    @BindView(R.id.tv_memory)
    TextView tv_memory;

    @BindView(R.id.logout)
    Button logout;

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {

        setTitle("设置");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initNetData() {

        rl_app_version.setOnClickListener(this);
        rl_aboutUs.setOnClickListener(this);
        rl_clean.setOnClickListener(this);
        rl_pay_pwd.setOnClickListener(this);
        rl_update_pwd.setOnClickListener(this);
        logout.setOnClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        tv_version.setText(UIUtils.getVerName(getApplicationContext()));
        try {
            String Cache = DataCleanManager.getTotalCacheSize(this);
            tv_memory.setText(Cache);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void customDestroy() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {


            case R.id.rl_app_version:

                GetNetCenter().GetNetCenter(RetrofitBuilder.CreateApi().version_check_android(ConstantsCode.version_check_android),
                        new Response() {
                            @Override
                            public void OK(Object data) {

                                VersionBean versionBean = (VersionBean) data;


                                PackageInfo packageInfo = null;
                                try {
                                    packageInfo = getApplicationContext()
                                            .getPackageManager()
                                            .getPackageInfo(getPackageName(), 0);

                                    if (!(packageInfo.versionCode + "").equals(versionBean.getData().getNumber())) {
                                        updateTip tip = new updateTip();

                                        tip.getUpdateInfo(SettingActivity.this, versionBean.getData().getNumber(), versionBean.getData().getUpgradeContent(), new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {

                                            }
                                        });
                                    }

                                } catch (PackageManager.NameNotFoundException e) {
                                    e.printStackTrace();
                                }


                            }

                            @Override
                            public void Fail(Throwable e) {

                            }

                            @Override
                            public void complete() {


                            }
                        });


                break;
            case R.id.rl_aboutUs:
                startActivity(new Intent(SettingActivity.this, AboutUsActivity.class));

                break;

            case R.id.rl_clean:
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setTitle("确定清除？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DataCleanManager.clearAllCache(getApplicationContext());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    tv_memory.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
                                    showToast("清除完成");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.show();
                break;
            case R.id.rl_update_pwd:
                startActivityForResult(new Intent(SettingActivity.this, ChangePasswordActivity.class), 1);
                break;
            case R.id.rl_pay_pwd:
                //支付密码
                startActivity(new Intent(SettingActivity.this, PaySettingsActivity.class));

                break;
            case R.id.logout:
                //退出登录
                userLogout();
                break;
        }
    }

    /**
     * 接口请求，退出登录
     */
    private void userLogout() {
        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().LOGOUT(ConstantsCode.staff_logout, SharedPreferencesUtil.getToken()), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        showToast("退出成功");
                        SharedPreferencesUtil.clearData();  //清除所有本地存储的数据
                        startActivity(new Intent(SettingActivity.this, LoginPwdActivity.class));
                        JPushInterface.deleteAlias(SettingActivity.this, 1);
                        MyApplication.removeAll();
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                        if (e.getMessage().contains("Code")) {
                            ErrorBean errorBean = new Gson().fromJson(e.getMessage(), ErrorBean.class);
                            showToast(errorBean.getMsg());
                        } else {
                            showToast(e.getMessage());
                        }
                    }

                    @Override
                    public void complete() {

                    }
                });
    }

    Handler handler = new Handler();
}
